What is @types/esquery?
@types/esquery provides TypeScript type definitions for the esquery library, which is used to query and match nodes in an ESTree-compliant AST (Abstract Syntax Tree). This package allows developers to use esquery with TypeScript, ensuring type safety and better development experience.
What are @types/esquery's main functionalities?
Querying AST Nodes
This feature allows you to query AST nodes using CSS-like selectors. In the example, it finds all Identifier nodes with the name 'myFunction' that are direct children of FunctionDeclaration nodes.
const esquery = require('esquery');
const ast = /* some ESTree-compliant AST */;
const matches = esquery(ast, 'FunctionDeclaration > Identifier[name="myFunction"]');
console.log(matches);
Complex Queries
This feature supports complex queries using pseudo-classes. In the example, it finds all FunctionDeclaration nodes that have an Identifier child node with the name 'myFunction'.
const esquery = require('esquery');
const ast = /* some ESTree-compliant AST */;
const matches = esquery(ast, 'FunctionDeclaration:has(Identifier[name="myFunction"])');
console.log(matches);
Combining Selectors
This feature allows combining multiple selectors to match different types of nodes. In the example, it finds all FunctionDeclaration and VariableDeclaration nodes in the AST.
const esquery = require('esquery');
const ast = /* some ESTree-compliant AST */;
const matches = esquery(ast, 'FunctionDeclaration, VariableDeclaration');
console.log(matches);
Other packages similar to @types/esquery
eslint
ESLint is a tool for identifying and reporting on patterns found in ECMAScript/JavaScript code. It can be extended with custom rules and plugins, and it uses an AST to analyze code. While ESLint is primarily focused on linting, it also provides an API for working with ASTs, similar to esquery.
babel-eslint
babel-eslint is a parser that allows ESLint to lint all valid Babel code. It converts Babel's AST into an ESTree-compliant AST, which can then be queried using tools like esquery. It is useful for projects using Babel to transform their code.
acorn
Acorn is a small, fast, JavaScript-based JavaScript parser. It produces an ESTree-compliant AST, which can be queried using esquery. Acorn is focused on parsing and does not provide querying capabilities out of the box, but it can be used in conjunction with esquery.